package com.engr.lyx.base.biz.repository;

import com.engr.lyx.base.client.entity.Permission;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PermissionRepository extends JpaRepository<Permission,Long> {

    /**
     * 查询所有权限
     * @Author:lyx
     * @return
     */
    @Query("SELECT DISTINCT p.perms FROM Permission p")
    List<String> findAllAuthorities();

    /**
     * @desc:  根据用户id查找所有权限
     * @Param: [userid]
     * @Author: lyx
     * @Date: 2019-01-23
     */
    @Query(value = "SELECT p.perms FROM permission p INNER JOIN role_permission rp ON p.id = rp.permission_id " +
            "INNER JOIN user_role ur ON ur.role_id = rp.role_id WHERE ur.user_id = ?1",nativeQuery = true)
    List<String> findPermsByUserId(Long userId);
}
